/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 13.09.12
 * Time: 14:23
 * To change this template use File | Settings | File Templates.
 */
package fgui.navigation {
import fgui.GuiComponent;

import fgui.control.TextButton;

import fgui.GuiComponent;

import flash.events.MouseEvent;
import flash.geom.Point;

import mx.collections.ArrayCollection;

/**
 * This class for creating tabbed panel (horizontal of vertical)
 *
 * @see TabbedPanel
 */
public class TabbedPanel extends GuiComponent {
    private var tabs:ArrayCollection = new ArrayCollection();
    private var mediumWidthTab:int = 100;

    /**
     *
     * @param coordinate - coordinate/position of component
     * @param size - width and height component
     */
    public function TabbedPanel(coordinate:Point, size:Point) {
        super(coordinate, size);

        renderColor = 0xcccccc;
        renderAlpha = 0.5;
        this.drawRoundedRectangle(
                renderColor,
                renderAlpha,
                new Point(-margin / 2, -margin / 2),
                new Point(size.x + margin, size.y + margin),
                new Point(20, 20));
    }

    public function getAllTabs():ArrayCollection {
        return this.tabs;
    }

    public function addTab(item:TextButton, callback:Function):void {
        item.setCoordinate(new Point(tabs.length * mediumWidthTab, 0));
        this.tabs.addItem(item);
        item.addEventListener(MouseEvent.CLICK, callback);
        addChild(item);

        // After adding new tab panel must be rebuild because summary width of all tabs is changed
        rebuildTabs();
    }

    public function removeAllTabs():void {
        this.tabs.removeAll();
    }

    private function rebuildTabs():void {
        var summaryTabsWidth:Number = tabs.length * mediumWidthTab;
        if (summaryTabsWidth >= this.getSize().x) {
            // copy all tabs

            var newMediumWidth:Number = this.getSize().x / tabs.length;
            var counter:int = 0;
            for each (var item:TextButton in tabs) {
                item.setCoordinate(new Point(counter * newMediumWidth, 0));
                item.setSize(new Point(newMediumWidth, item.getSize().y));
                counter++;
            }
        } else {
            //
        }
    }
}
}
